{ INFORMATICS.MCCME.RU
  Problem statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=3011
  Verdict: Accepted
}
uses 
	SysUtils, Math;
 
type
	al=array of int64;
 
var  	
	n,m,kk,v,tt,ans,x,y,z:int64;
	level,vert,ind,a,tr:al;
	g:array of array of int64;
	i:longint;
 
procedure swap(var a,b:int64);
 
var
	t:int64;
 
begin
t:=a;
a:=b;
b:=t;
end;
 
function minI(x,y:longint):longint;
begin
if level[x]<level[y] then result:=x else result:=y;
end;
 
procedure init(var t,ver:al);
 
var
	i,tt,kk:longint;
 
begin
tt:=length(ver);
setlength(t,tt*2+2);
kk:=2*tt-1;
for i:=tt to kk do
	t[i]:=ver[i-tt];
for i:=tt-1 downto 1 do
	t[i]:=minI(t[2*i],t[2*i+1]);
end;
 
function find(l,r:int64):int64;
 
var
	tt,mn:int64;
 
begin
if l>r then swap(l,r);
tt:=length(vert);
inc(l,tt);
inc(r,tt);
mn:=tr[l];
while l<=r do begin
	if l and 1<>0 then begin
		mn:=mini(mn,tr[l]);
		inc(l);
	end;
	if r and 1=0 then begin
		mn:=mini(mn,tr[r]);
		dec(r);
	end;
	l:=l shr 1;
	r:=r shr 1;
end;
result:=mn;
end;
 
procedure dfs(v,pr,lev:longint);
 
var
	tt,i,kk:longint;
 
begin
level[v]:=lev;
tt:=length(vert);
setlength(vert,tt+1);
vert[tt]:=v;
ind[v]:=tt;
kk:=length(g[v])-1;
for i:=0 to kk do begin
	if pr<>g[v][i] then begin
		dfs(g[v][i],v,lev+1);
		tt:=length(vert);
		setlength(vert,tt+1);
		vert[tt]:=v;
	end;
end;
end;
 
begin
reset(input,'input.txt');
rewrite(output,'output.txt');
read(n,m);
setlength(g,n+1);
setlength(level,n+1);
setlength(ind,n+1);
setlength(a,2*m+2);
kk:=n-1;
for i:=1 to kk do begin
	read(v);
	tt:=length(g[i]);
	setlength(g[i],tt+1);
	g[i][tt]:=v;
	tt:=length(g[v]);
	setlength(g[v],tt+1);
	g[v][tt]:=i;
end;
dfs(0,-1,0);
init(tr,vert);
read(a[1],a[2]);
kk:=2*m;
ans:=0;
v:=0;
read(x,y,z);
for i:=3 to kk do a[i]:=(x*a[i-2]+y*a[i-1]+z) mod n;
for i:=1 to m do begin
	v:=find(ind[(a[i*2-1]+v) mod n],ind[a[2*i]]);
	inc(ans,v);
end;
write(ans);
end.